package damon.utils;

public class BytesToData {
    static public String bytesToString(byte[] bytes) {
        return new String(bytes);
    }

    static public short bytesToShort(byte[] bytes) {
        return (short) (0xff00 & bytes[0] << 8 | (0xff & bytes[1]));
    }

    static public int bytesToInt(byte[] bytes) {

        return (0xff000000 & (bytes[0] << 24)) |
                (0x00ff0000 & (bytes[1] << 16)) |
                (0x0000ff00 & (bytes[2] << 8)) |
                (0x000000ff & bytes[3]);
    }

    static public long bytesToLong(byte[] bytes) {
        return (0xff00000000000000L & ((long) bytes[0] << 56)) |
                (0x00ff000000000000L & ((long) bytes[1] << 48)) |
                (0x0000ff0000000000L & ((long) bytes[2] << 40)) |
                (0x000000ff00000000L & ((long) bytes[3] << 32)) |
                (0x00000000ff000000L & ((long) bytes[4] << 24)) |
                (0x0000000000ff0000L & ((long) bytes[5] << 16)) |
                (0x000000000000ff00L & ((long) bytes[6] << 8)) |
                (0x00000000000000ffL & (long) bytes[7]);
    }

    static public float bytesToFloat(byte[] bytes) {
        return Float.intBitsToFloat(bytesToInt(bytes));
    }

    static public double bytesToDouble(byte[] bytes) {
        return Double.longBitsToDouble(bytesToLong(bytes));
    }


}
